package com.future.mayi.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.TextView;

import com.future.mayi.R;
import com.future.mayi.activity.AddAddressActivity;
import com.future.mayi.model.AddressModel;
import com.future.mayi.model.interfaces.AddressListener;

import java.util.List;

public class AddressAdapter extends CommonAdapter<AddressModel> {

    private Activity activity;

    public AddressAdapter(Activity activity, int textViewResourceId, List<AddressModel> objects) {
        super(activity, textViewResourceId, objects);
        this.activity = activity;
    }

    private AddressListener listener;

    @Override
    public void convert(ViewHolder helper, final AddressModel item) {
        final int position = helper.getPosition();
        helper.setText(R.id.tv_name, item.getName());
        helper.setText(R.id.tv_phone, item.getPhone());
        helper.setText(R.id.tv_address, item.getLocation() + item.getDetail());
        TextView tvDefault = helper.getView(R.id.tv_default);

        Drawable drawable = mContext.getResources().getDrawable(R.drawable.img_select_f);
        if (item.getIsdefault() == 1)
            drawable = mContext.getResources().getDrawable(R.drawable.img_select_t);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvDefault.setCompoundDrawables(drawable, null, null, null);

        tvDefault.setTag(item);
        if (listener != null)
            tvDefault.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.clickTop((AddressModel) v.getTag());
                }
            });

        helper.getView(R.id.tv_edt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, AddAddressActivity.class);
                intent.putExtra("position", position);
                intent.putExtra("model", getItem(position));
                activity.startActivityForResult(intent, 210);
            }
        });


        TextView tvDelete = helper.getView(R.id.tv_delete);
        tvDelete.setTag(item);
        if (listener != null)
            tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.clickDelete((AddressModel) v.getTag());
                }
            });
    }

    public void setListener(AddressListener listener) {
        this.listener = listener;
    }
}
